/*
 * ComboBoxTableModel.java
 *
 * Created on 9 de febrero de 2008, 05:56 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package prototipo;

import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Billy
 */
public class ComboBoxTableModel extends AbstractTableModel {    
  // Implementation of TableModel interface 
    
  ComboBoxTableModel(Object[][] data, String[] validStates){
      this.data = data;  
      this.validStates = validStates;
  }  
  
  public int getRowCount() {
    return data.length;
  }

  public int getColumnCount() {
    return COLUMN_COUNT;
  }

  public Object getValueAt(int row, int column) {
    return data[row][column];
  }

  public Class getColumnClass(int column) {
    return (data[0][column]).getClass();
  }

  public String getColumnName(int column) {
    return columnNames[column];
  }

  public boolean isCellEditable(int row, int column) {
    return column == 3;/*1*/
  }

  public void setValueAt(Object value, int row, int column) {
    if (isValidValue(value)) {
      data[row][column] = value;
      fireTableRowsUpdated(row, row);
    }
  }

  // Extra public methods
  public static String[] getValidStates() {
    return validStates;
  }

  // Protected methods
  protected boolean isValidValue(Object value) {
    if (value instanceof String) {
      String sValue = (String)value;

      for (int i = 0; i < validStates.length; i++) {
        if (sValue.equals(validStates[i])) {
          return true;
        }
      }
    }
    return false;
  }

  protected static final int COLUMN_COUNT = 5;
  protected static String[] validStates;
  protected Object[][] data;     
  protected static final String[] columnNames = {
    "idJuego", "Blancas", "Negras", "Resultado", "Registrado"
  };

}

